﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using QunYing.BLL;
using System.ServiceModel;

namespace Client
{
    public partial class FrmGoodsManager : BaseChildForm
    {
        public static FrmGoodsManager frm = null;
        public FrmGoodsManager()
        {
            InitializeComponent();
            this.sharpGrid1.AddPopupMenu(new ToolStripSeparator(), null);
            this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("删除选择商品"), RemoveGoods);
            frm = this;
            cbxDown.SelectedIndex = 1;
        }

        private void FrmGoodsManager_Load(object sender, EventArgs e)
        {
            CbxBind();
            QueryData();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        private void CbxBind()
        {
            QunYing.BLL.Category category = new Category();
            DataTable dt = category.GetList("经销商编号='" + GlobalInfo.UserLoginInfo.用户ID
                + "' AND 类别ID NOT IN (SELECT 父类别ID FROM dbo.Category where 经销商编号='"
                + GlobalInfo.UserLoginInfo.用户ID + "')").Tables[0];
            DataTable dtAll = new DataTable();
            dtAll.Columns.Add("类别ID");
            dtAll.Columns.Add("类别名称");
            dtAll.Rows.Add("", "");
            foreach (DataRow dr in dt.Rows)
            {
                dtAll.Rows.Add(dr["类别ID"],dr["类别名称"]);
            }
            cbxCategory.Properties.DataSource = dtAll;
            cbxCategory.Properties.ValueMember = "类别ID";
            cbxCategory.Properties.DisplayMember = "类别名称";
            cbxCategory.ItemIndex = 0;
        }
        public void QueryData()
        {
            QunYing.BLL.GoodsInfo goodsInfo = new GoodsInfo();
            DataTable dt = goodsInfo.GetList(GetFiter()).Tables[0];
            sharpGrid1.DataSource = dt;

            GridView view = this.sharpGrid1.MainView as GridView;
            view.Columns["商品ID"].Visible = false;
            view.Columns["经销商编号"].Visible = false;
            view.Columns["显示顺序"].Visible = false;
            view.Columns["类别ID"].Visible = false;
        }

        private string GetFiter()
        {
            StringBuilder strFiter = new StringBuilder();
            strFiter.Append(" 经销商编号='" + GlobalInfo.UserLoginInfo.用户ID + "'");
            strFiter.Append(" AND 商品名称 like '%" + txtGoodsName.Text.Trim() + "%'");
            strFiter.Append(" AND 商品品牌 like '%" + txtBrand.Text.Trim() + "%'");
            if (cbxCategory.EditValue.ToString() != "")
            {
                strFiter.Append(" AND 类别ID = '" + cbxCategory.EditValue + "'");
            }
            if (cbxZone.Text == "热门推荐")
            {
                strFiter.Append(" AND 是否推荐=1");
            }
            if (cbxZone.Text == "特价专区")
            {
                strFiter.Append(" AND 是否折价=1");
            }
            if (cbxZone.Text == "积分兑换")
            {
                strFiter.Append(" AND 是否兑换=1");
            }
            if (cbxDown.Text == "是")
            {
                strFiter.Append(" AND 是否下架=1");
            }
            if (cbxDown.Text == "否")
            {
                strFiter.Append(" AND 是否下架=0");
            }
            return strFiter.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmAddGoods frm = new FrmAddGoods();
            frm.OPERTYPE = "ADD";
            frm.ShowDialog();
        }

        private void RemoveGoods(object sender, EventArgs e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            if (selectedRows.Length == 0)
            {
                GlobalInfo.MsgBoxShowError("请选择你要删除的商品");
                return;
            }
            else
            {
                if (XtraMessageBox.Show(GlobalInfo.UserLookAndFeel, "确认删除选择商品吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                    return;
                foreach (var v in selectedRows)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;

                    QunYing.BLL.OrderDetail orderDetail = new OrderDetail();
                    DataSet ds = orderDetail.GetList("商品ID='" + row.Row["商品ID"] + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        GlobalInfo.MsgBoxShowError("该商品已出售,不能删除该商品!");
                        return;
                    }

                    QunYing.BLL.GoodsInfo goodsInfo = new GoodsInfo();
                    bool b = goodsInfo.Delete(Convert.ToInt32(row.Row["商品ID"]));
                    if (b)
                    {
                        QunYing.BLL.GoodsImg goodsImg = new GoodsImg();
                        DataTable dt = goodsImg.GetGoodsImg(row.Row["商品ID"].ToString());
                        foreach (DataRow dr in dt.Rows)
                        {
                            goodsInfo.Delete(Convert.ToInt32(dr["图片ID"]));

                            try
                            {
                                QunYing.Common.IImageOperator opera = QunYing.Common.ClientProxyFactory.GetProxy<QunYing.Common.IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationVip);
                                opera.DeleteImg(new string[] { dr["图片路径"].ToString() });
                                (opera as ICommunicationObject).Close();
                            }
                            catch (Exception ex)
                            {
                                QunYing.Common.Loger.Log.Error("删除图片发生错误:" + ex.Message);
                            }
                        }

                    }

                }

                QueryData();
            }
        }

        private void sharpGrid1_RowDoubleClickEvent(object sender, Spore.Controls.RowDoubleClickEventArg e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    QunYing.Model.GoodsInfo model = new QunYing.BLL.GoodsInfo().GetModel(Convert.ToInt32(row.Row["商品ID"]));
                    FrmAddGoods frm = SingleObjManager.GetSingleObj<FrmAddGoods>();
                    frm.OPERTYPE = "EDIT";
                    frm.Text = "修改商品";
                    frm.editModel = model;
                    frm.editDt=new QunYing.BLL.GoodsImg().GetGoodsImg(row.Row["商品ID"].ToString());
                    frm.ShowDialog();
                    break;
                }
            }
        }

        public override void BindData()
        {
            QueryData();
        }
    }
}